package bud.vo;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;

import lombok.Data;

@Data
public class MemberDTO {
	private Integer mem_no;
	private String mem_email;
	private String mem_pass;
	private String mem_gender;
	private String mem_name;
	private String mem_message;
	private MultipartFile mem_image;
	private byte[] mem_photo;
	private String mem_scaninfo;
	private String mem_sec_tofriend;
	private String mem_sec_topeople;
	
	private List<MemberDTO> friendList;
	
	public void setMem_image(MultipartFile mem_image) throws IOException{
		this.mem_image = mem_image;
//		mem_photo = mem_image.getBytes();
		if (mem_image.isEmpty()){
			return;
		}
		
		// 이미지 리사이즈
		BufferedImage originalImg = ImageIO.read(mem_image.getInputStream());
		int maxLength = 500;
		int destWidth = 0;
		int destHeight = 0;
		if (originalImg.getWidth() > maxLength || originalImg.getHeight() > maxLength){	
			if (originalImg.getWidth() > originalImg.getHeight()){
				destWidth = maxLength;
				destHeight = (int)(originalImg.getHeight()/(double)originalImg.getWidth() * maxLength);
			} else {
				destWidth = (int)(originalImg.getWidth()/(double)originalImg.getHeight() * maxLength);
				destHeight = maxLength;
			}
		} else {
			destWidth = originalImg.getWidth();
			destHeight = originalImg.getHeight();
		}
		
		BufferedImage resizeImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = resizeImg.createGraphics();
		g.drawImage(originalImg, 0, 0, destWidth, destHeight, null);
		g.dispose();

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ImageIO.write(resizeImg, "jpg", os);
		mem_photo = os.toByteArray();
		os.close();

	}
	
	public String getMem_photoStr(){
		return mem_photo==null?null:Base64.encodeBase64String(mem_photo);
	}
	
}
